package collector

import (
	"fmt"
	"os"
	"time"

	"sysinfo/internal/config"
	"sysinfo/internal/types"
)

// Collect 收集所有请求的系统信息
func Collect(cfg *config.Config) (*types.SystemInfo, error) {
	info := &types.SystemInfo{
		Timestamp: time.Now(),
	}

	var err error

	// Collect system information
	if cfg.ShouldCollect("system") {
		info.System, err = CollectSystem()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting system info: %v\n", err)
		}
	}

	// Collect CPU information
	if cfg.ShouldCollect("cpu") {
		info.CPU, err = CollectCPU()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting CPU info: %v\n", err)
		}
	}

	// Collect memory information
	if cfg.ShouldCollect("memory") {
		info.Memory, err = CollectMemory()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting memory info: %v\n", err)
		}
	}

	// Collect disk information
	// Note: If SMART is requested, we need to collect disk data to include SMART info
	if cfg.ShouldCollect("disk") || cfg.ShouldCollect("smart") {
		info.Disk, err = CollectDisk(cfg.ShouldCollect("smart"))
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting disk info: %v\n", err)
		}
	}

	// Collect network information
	if cfg.ShouldCollect("network") {
		info.Network, err = CollectNetwork()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting network info: %v\n", err)
		}
	}

	// Collect ports usage information
	if cfg.ShouldCollect("ports") {
		info.Ports, err = CollectPorts()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting ports info: %v\n", err)
		}
	}

	// Collect process information
	if cfg.ShouldCollect("process") {
		info.Processes, err = CollectProcesses()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting process info: %v\n", err)
		}
	}

	// Collect GPU information
	if cfg.ShouldCollect("gpu") {
		info.GPU, err = CollectGPU()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting GPU info: %v\n", err)
		}
	}

	// Collect battery information
	if cfg.ShouldCollect("battery") {
		info.Battery, err = CollectBattery()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting battery info: %v\n", err)
		}
	}

	// Collect recent activity information
	if cfg.ShouldCollect("activity") {
		info.Activity, err = CollectActivity()
		if err != nil && cfg.Verbose {
			fmt.Fprintf(os.Stderr, "Error collecting activity info: %v\n", err)
		}
	}

	return info, nil
}
